package OrderService

import (
	"context"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/entity"

	"github.com/gogf/gf/frame/g"
	"time"
)

// OneDayConfirmOrder 一天执行一次，确认收货，30天前的订单
func OneDayConfirmOrder() {
	ctx := context.Background()
	var oList []*entity.Order
	var dTime int64 = 30 * 86400
	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "order").Where(
		"status = 4 AND create_at < ?",
		time.Now().Unix()-dTime).Structs(&oList)
	if err != nil {
		g.Log().Async().Cat("OneDayConfirmOrder").Error("失败：", err)
		return
	}
	if oList == nil {
		g.Log().Async().Cat("OneDayConfirmOrder").Info("没有需要处理的订单")
		return
	}
	for _, item := range oList {
		jieguo := CompleteOrder(ctx, item.Id)
		g.Log().Async().Cat("OneDayConfirmOrder").Info("结果：", jieguo)
	}
}
